﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Analyst3D;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;

namespace Homework5
{
    public partial class Form4 : Form
    {
        IMap pMap;
        IFeatureLayer pFlayer;
        public IStyleGalleryItem m_styleGalleryItem = null;
        public IColorRamp colorRamp = null;
        public static Form4 form4;
        string strRendererField;//分级字段 
        string strNormalizeField;//标准化字段
        int gClassCount;//分类数
        string strClassifyMethod;//分类方法

        public Form4()
        {
            form4 = this;
            InitializeComponent();
        }
        public Form4(IMap map)
        {
            form4 = this;

            pMap = map;
            InitializeComponent();
            
        }
        private void Form4_Load(object sender, EventArgs e)
        {
            //为图层添加下拉框内容
            comboBox1.Items.Clear();
            for (int i = 0; i < pMap.LayerCount; i++)
            {
                comboBox1.Items.Add(pMap.get_Layer(i).Name);

            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBox1.SelectedIndex;
            IFeatureLayer select_flayer = pMap.get_Layer(index) as IFeatureLayer;
            IFeatureClass pFClass = select_flayer.FeatureClass;
            IFields pFields = pFClass.Fields;


            //储存属性字段名
            for (int i = 0; i < pFields.FieldCount; i++)
            {
                comboBox3.Items.Add(pFields.get_Field(i).Name);
                comboBox2.Items.Add(pFields.get_Field(i).Name);
            }
        }
        //1、先把需符号化的图层里的数据分级
        double[] gClassbreaks = null;
        private void classify(IFeatureLayer pFeatureLayer)
        {      
            IFeatureClass featureClass = pFeatureLayer.FeatureClass;
            ITable pTable = (ITable) featureClass;
            ITableHistogram pTableHistogram = new BasicTableHistogramClass();
            IBasicHistogram pHistogram = (IBasicHistogram) pTableHistogram;
            pTableHistogram.Field = strRendererField;
            if (strNormalizeField.ToLower() != "none")
                         pTableHistogram.NormField = strNormalizeField;
            pTableHistogram.Table = pTable;
            object dataFrequency, dataValues;
            pHistogram.GetHistogram(out dataValues, out dataFrequency);
            IClassifyGEN pClassify = new NaturalBreaksClass();
            switch (strClassifyMethod)
            {
                case "等间隔分类":
                    pClassify = new EqualIntervalClass(); break;
                case "分位数分类":
                    pClassify = new QuantileClass(); break;
                case "自然裂点分类":
                    pClassify = new NaturalBreaksClass(); break;
                case "几何间隔分类":
                    pClassify = new GeometricalIntervalClass(); break;
            }
            int numDesiredClasses = gClassCount;
            pClassify.Classify(dataValues, dataFrequency, ref numDesiredClasses);
            gClassbreaks = (double[])pClassify.ClassBreaks;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            strRendererField = comboBox2.Text;
            strNormalizeField = comboBox3.Text;
            gClassCount = (int)numericUpDown1.Value;
            strClassifyMethod = comboBox4.Text;
            classify(pMap.get_Layer(0) as IFeatureLayer);
            colorRamp.Size = gClassbreaks.Length;
            bool createRamp;
            colorRamp.CreateRamp(out createRamp);
            IEnumColors enumColors = colorRamp.Colors;
            enumColors.Reset();
            pFlayer = pMap.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFClass = pFlayer.FeatureClass;
            ISymbol symbol = null;
            IColor pColor = enumColors.Next();
            switch (pFClass.ShapeType)
            {
                case esriGeometryType.esriGeometryPoint:
                    symbol = CreatePointSymbol(pColor);
                    break;
                case esriGeometryType.esriGeometryPolyline:
                    symbol = CreatePolylineSymbol(pColor);
                    break;
                case esriGeometryType.esriGeometryPolygon:
                    symbol = CreatePolygonSymbol(pColor);
                    break;
            }
            IClassBreaksRenderer pRender = new ClassBreaksRendererClass();
            pRender.BreakCount = gClassbreaks.Length;
            pRender.Field = strRendererField;
            for (int i = 0; i < gClassbreaks.Length; i++)
            {
                pRender.set_Break(i, (double)gClassbreaks.GetValue(i));
                pRender.set_Symbol(i, symbol);
            }

            IGeoFeatureLayer pGeoLyr = (IGeoFeatureLayer)pMap.get_Layer(0);
            pGeoLyr.Renderer = (IFeatureRenderer)pRender;

            Form1.form1.Form1Update();
        }

        ISymbol CreatePointSymbol(IColor pColor)
        {
            ISimpleMarkerSymbol simpleMarkerSymbol =
                new SimpleMarkerSymbolClass();
            simpleMarkerSymbol.Color = pColor;
            return simpleMarkerSymbol as ISymbol;
        }
        ISymbol CreatePolylineSymbol(IColor pColor)
        {
            ISimpleLineSymbol simpleLineSymbol = 
                new SimpleLineSymbolClass();
            simpleLineSymbol.Color = pColor;
            return simpleLineSymbol as ISymbol;
        }
        ISymbol CreatePolygonSymbol(IColor pColor)
        {
            ISimpleFillSymbol simpleFillSymbol =
                new SimpleFillSymbolClass();
            simpleFillSymbol.Color = pColor;
            return simpleFillSymbol as ISymbol;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form5 form5 = new Form5();
            form5.Show();
        }

        

        



    }
}
